using System;
using System.Collections.Generic;
using Script.Library.Ability;
using UnityEngine;

public static class CreatureLoader
{
	private class HandInfo
	{
		public int i;

		public Progress.ImprovementInfo info;

		public HandInfo(int i, Progress.ImprovementInfo info)
		{
			this.i = i;
			this.info = info;
		}
	}

	public static Creature creature;

	private static Transform parent;

	private static Progress.ImprovementInfo info;

	public static List<Hand> claws;

	public static List<Hand> spikes;

	public static void Fill(Creature creature, bool abilities)
	{
		CreatureLoader.creature = creature;
		parent = creature.model;
		claws = new List<Hand>();
		spikes = new List<Hand>();
		Progress.ApplyBodyShape(creature.head.parent.GetChildren(9));
		creature.body.transform.GetChild(0).gameObject.layer = 11 - Progress.foodType * 2;
		creature.meatEater = Progress.foodType == 0;
		creature.mouthAnimation = Progress.ApplyFoodType(creature.head).GetComponent<Animation>();
		ApplyImprovements();
		if (abilities)
		{
			ApplyAbilities(Progress.abilities, Progress.passiveAbility);
		}
	}

	public static void ApplyAbilities(Progress.Ability[] abilities, Progress.Ability passiveAbility)
	{
		int num = abilities.Length;
		creature.abilities = new Ability[abilities.Length];
		for (int i = 0; i < num; i++)
		{
			Progress.Ability ability = abilities[i];
			Ability ability2 = (Ability)Activator.CreateInstance(Type.GetType("Script.Library.Ability." + ability.name), creature, ability.level);
			creature.abilities[i] = ability2;
		}
		switch (passiveAbility.name)
		{
		case "Swimming":
			creature.EnableSwimming(passiveAbility.level);
			break;
		case "Spider":
			creature.webWalker = true;
			break;
		case "Antidote":
			creature.poisonResistence = (float)(1 - passiveAbility.level) * 0.5f;
			break;
		}
	}

	private static void ApplyImprovements()
	{
		List<HandInfo> list = new List<HandInfo>();
		int num = 0;
		int num2 = Progress.improvementInfos.Length;
		for (int i = 0; i < num2; i++)
		{
			info = Progress.improvementInfos[i];
			info.posY *= -1f;
			Transform transform = CreateBodyPart();
			Progress.ApplyImprovement(transform, parent, info);
			if (info.hand)
			{
				list.Add(new HandInfo(num, info));
			}
			num += 1 + info.copy.ToInt();
			info.posY *= -1f;
		}
		for (int j = 2; j < parent.childCount; j++)
		{
			Transform child = parent.GetChild(j);
			child.rotation = Quaternion.Euler(child.eulerAngles);
		}
		list.Reverse();
		foreach (HandInfo item in list)
		{
			if (item.info.copy)
			{
				ApplyHand(item.i + 1, item.info, 1 + item.info.copy.ToInt());
			}
			ApplyHand(item.i, item.info, 1 + item.info.copy.ToInt());
		}
		creature.legsAnimation = Main.GetAllChildrenWithTag<Animation>(parent, "Leg");
	}

	private static void ApplyHand(int i, Progress.ImprovementInfo info, int bonus)
	{
		Hand component = parent.GetChild(i + 1).GetComponent<Hand>();
		Transform transform = (component.child = parent.GetChild(i + bonus + 1));
		transform.SetParent(component.parent);
		transform.localPosition = Vector3.zero;
		((!(transform.tag == "Claw")) ? spikes : claws).Add(component);
	}

	private static Transform CreateBodyPart()
	{
		return UnityEngine.Object.Instantiate(Info.Body.allParts[info.name]).transform;
	}
}
